package com.ohc.leetcode.字符串;


import java.util.ArrayDeque;
import java.util.Stack;

// 2022年4月13日14:55:40
// https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/
public class 删除字符串中的所有相邻重复项1047 {

    public static String removeDuplicates(String s) {
        if(s==null){
            return null;
        }
        char c = ' ';
        ArrayDeque<Character> stack = new ArrayDeque<>();
        for (int i = 0; i < s.length(); i++) {
            c=s.toCharArray()[i];
            if (stack.isEmpty() || stack.peek() !=c ) {
                stack.push(c);
            } else {
                stack.pop();
            }


        }
        String str = "";
        while (!stack.isEmpty()) {
            str =  stack.pop() + str;
        }
        return str;
    }

    public static void main(String[] args) {


        String str = removeDuplicates("abbaca");
        System.out.println("str = " + str);

    }
}
